home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Utilities / Installers / InstallerMaker™ 3.0 / Preinstalled version / Customizing InstallerMaker / Sample Code / ILoc Example / ScriptAddILoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-21  |  4.6 KB  |  199 lines  |  [TEXT/MMCC]

  1. /* ****************************************************************
  2.  
  3.         ScriptAddILoc.c
  4.                     Written in Metrowerks C by Robert Thorne (rmt).
  5.                     
  6.         Copyright © 1994-1995 Aladdin Systems, Inc.
  7.         All Rights Reserved.
  8.  
  9.  
  10.         Quick ILoc for Scripting Additions.  Will create the folder if it is missing
  11.         
  12.         This file demostrates how to write the ILoc code resource that
  13.         you build into an InstallerMaker 3.0 locations module.
  14.         
  15.         Last Revised: 8/3/95, rmt.
  16.         
  17. ********************************************************************/
  18.  
  19.  
  20. #include "A4Globals.h" /* Quick trick to let us compile and test easier */
  21.  
  22. #if !GLOBALS_ALWAYS_AVAILABLE
  23.     #define ILOC_MAIN main
  24. #endif
  25.  
  26. #include <Folders.h>
  27. #include "IMExtensionsFor3_0.h"
  28.  
  29.  
  30. // Prototype for the routine that does the real work
  31. static short DoILoc    (    IMEnvironsRec *environs,
  32.                         short *destVol,
  33.                         long *destDir,
  34.                         short *killDestinationPrompt,
  35.                         unsigned long *refCon )    ;
  36.  
  37.  
  38. // Prototypes for a few utilities.
  39.  
  40. static unsigned char *pStrcpy(unsigned char *dest, unsigned char *src) ;
  41. static Boolean    FileOrDirExists (     short vRefNum, long dirID, Str255 name,
  42.                             OSType *typePtr, Boolean *isFolder ) ;
  43.                             
  44. //**** Implementation ******
  45.  
  46.  
  47. // Here's a shell for writing an ILoc 256:
  48.  
  49. pascal short ILOC_MAIN (    IMEnvironsRec *environs,
  50.                         short *destVol,
  51.                         long *destDir,
  52.                         short *killDestinationPrompt,
  53.                         unsigned long *refCon )    
  54. {
  55.     long            oldA4 ;
  56.     short            rslt ;
  57.  
  58.     // If we're running as a resource, we establish our A4 world here
  59.     MAIN_SETUP_GLOBALS(oldA4) ;
  60.     
  61.     // Call our routine
  62.     rslt = DoILoc ( environs, destVol, destDir, killDestinationPrompt, refCon ) ;
  63.     
  64.     MAIN_RESTORE_GLOBALS(oldA4) ; // Note we don't return until *after* this macro!!
  65.     
  66.     return rslt ;
  67.     
  68. }
  69.  
  70.  
  71. static short DoILoc    (    IMEnvironsRec *environs,
  72.                         short *destVol,
  73.                         long *destDir,
  74.                         short *killDestinationPrompt,
  75.                         unsigned long *refCon )
  76. {
  77.     FSSpec    spec ;
  78.     OSType    itsType ;
  79.     long    initDir ;
  80.     long    gestaltResult ;
  81.     short    initVol ;
  82.     Boolean    isFldr ;
  83.     OSErr    err ;
  84.     
  85.     // Worse case values for System 6
  86.     *destVol = -1 ;
  87.     *destDir = 2 ;
  88.     
  89.     // Make sure we're running under System 7.x.  I'll test for the FSSpec calls.
  90.     err = Gestalt (gestaltFSAttr, &gestaltResult ) ;
  91.     
  92.         
  93.     if ( err || !(gestaltResult & (1 << gestaltHasFSSpecCalls))) 
  94.         return  kILoc_SkipThisItem ; 
  95.         
  96.     
  97.     // First, let's get a reasonable default
  98.     err = FindFolder ( environs->userSysVol, kTrashFolderType,
  99.                        kDontCreateFolder, destVol, destDir ) ;
  100.     
  101.     // Get the extensions folder for the target
  102.     err = FindFolder ( environs->userSysVol, kExtensionFolderType,
  103.                        kDontCreateFolder, &initVol, &initDir ) ;
  104.  
  105.     if ( err )
  106.         return kILoc_SkipThisItem ;
  107.         
  108.     err = FSMakeFSSpec ( initVol, initDir, "\p:Scripting Additions", &spec ) ;
  109.     
  110.     if ( err == fnfErr )
  111.     {
  112.         // Create it
  113.         err = FSpDirCreate ( &spec, smSystemScript, &initDir ) ;
  114.         
  115.         if (!err )
  116.             *destDir = initDir ;
  117.     }
  118.     else if (!err)
  119.     {
  120.         err = FileOrDirExists ( spec.vRefNum, spec.parID, spec.name, &itsType, &isFldr ) ;
  121.         
  122.         if ( isFldr ) // we're ok
  123.         {
  124.             //Quick trick to get the dirID
  125.             err = FSMakeFSSpec ( spec.vRefNum, spec.parID,
  126.                                  "\p:Scripting Additions:Bogus", &spec ) ;
  127.                                  
  128.             if (err == fnfErr)
  129.                 err = noErr ;
  130.                 
  131.             initDir = spec.parID ;
  132.         }
  133.         else
  134.             err = -1 ;
  135.     }
  136.     
  137.     
  138.     // If no error, we have something good to return
  139.     if ( !err )
  140.     {
  141.         *destVol = initVol ;
  142.         *destDir = initDir ;
  143.         
  144.         return kILoc_UseParameters ;
  145.     }
  146.     else
  147.         return kILoc_SkipThisItem ;
  148.     
  149. }
  150.  
  151.  
  152. // Utility routines...
  153.  
  154. static Boolean    FileOrDirExists (     short vRefNum, long dirID, Str255 name,
  155.                             OSType *typePtr, Boolean *isFolder )
  156. {
  157.     // I do this by calling PBGetCatInfo, and checking ioFlAttrib
  158.     
  159.     CInfoPBRec        pb ; 
  160.     OSErr            err ; 
  161.     Str255            copyOfString ; // We don't want this walked on
  162.     StringPtr        nameParam = nil ;
  163.     
  164.     if ( name )
  165.         nameParam = pStrcpy ( copyOfString, name ) ; 
  166.     
  167.     // We need to set up a parameter block for a call to PBGetCatInfo.  This
  168.     // is documented in IM IV-155
  169.     
  170.     pb.hFileInfo.ioCompletion = nil ; // We intend to call the function synchronously
  171.     pb.hFileInfo.ioNamePtr = nameParam ;
  172.     pb.hFileInfo.ioVRefNum = vRefNum ;
  173.     pb.hFileInfo.ioDirID = dirID ;
  174.     pb.hFileInfo.ioFDirIndex = 0 ;     // 0 indicates we are interested in the file or
  175.                                       // directory named in ioNamePtr
  176.                           
  177.     err = PBGetCatInfoSync ( &pb ) ; 
  178.     
  179.     if ( err == noErr )
  180.     {
  181.         // First of all, we exist. First, see if we're a directory
  182.         
  183.         *isFolder = pb.hFileInfo.ioFlAttrib & ioDirMask ? true : false ;
  184.         
  185.         if ( !*isFolder )
  186.             *typePtr = pb.hFileInfo.ioFlFndrInfo.fdType ;
  187.         
  188.     }
  189.     
  190.     return err == noErr ? true : false ;
  191. }    
  192.  
  193.  
  194. static unsigned char *pStrcpy(unsigned char *dest, unsigned char *src)
  195. {
  196.     BlockMoveData(src, dest, (long) *src + 1); 
  197.     return (dest);
  198. }
  199.